package Shangji1;

import java.util.Scanner;

/**
 * 测试类
 */
public class CalculatorTest {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        try {
            // 获取用户输入
            System.out.print("请输入第一个数字: ");
            double num1 = scanner.nextDouble();

            System.out.print("请输入运算符 (+, -, *, /): ");
            String operator = scanner.next();

            System.out.print("请输入第二个数字: ");
            double num2 = scanner.nextDouble();

            // 使用工厂创建操作实例
            Operation operation = OperationFactory.createOperation(operator);

            // 执行计算
            double result = operation.calculate(num1, num2);

            // 输出结果
            System.out.println("计算结果: " + num1 + " " + operator + " " + num2 + " = " + result);

        } catch (IllegalArgumentException e) {
            System.out.println("错误: " + e.getMessage());
        } catch (ArithmeticException e) {
            System.out.println("计算错误: " + e.getMessage());
        } catch (Exception e) {
            System.out.println("输入错误: " + e.getMessage());
        } finally {
            scanner.close();
        }
    }
}